<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="800px">
      <div class="stock-change-form-title">商品名称：{{ state.ruleForm ? state.ruleForm.commodityName : '' }} |
        库存数量：{{ state.ruleForm ? state.ruleForm.quantity : '' }}
      </div>
      <el-form ref="userDialogFormRef" :model="state.stockChangeForm" :rules="rules" label-width="100px" size="default">
        <el-row :gutter="35">
          <el-col :span="24" class="mb20">
            <el-form-item label="业务场景：" prop="scene">
              <el-select v-model="state.stockChangeForm.scene" class="filter-item" clearable placeholder="业务场景">
                <el-option :value="6" label="手动调整"/>
                <el-option :value="5" label="报损"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="调整数量：" prop="quantity">
              <el-input v-model="state.stockChangeForm.quantity" placeholder="调整数量" type="number"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="调整原因" prop="remark">
              <el-input v-model="state.stockChangeForm.remark" maxlength="200"
                        placeholder="请输入调整原因"
                        type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button size="default" type="primary" @click="onSubmit(userDialogFormRef)">
						修改库存
					</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="systemUserDialog" setup>
import {nextTick, onMounted, reactive, ref} from 'vue';
import type {FormRules} from 'element-plus';
import {ElMessage} from 'element-plus';
import {stockApi} from "/@/api/store/stock";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    commodityName: '', // 商品名称
    quantity: '', // 调整数量
    commodityId: '',
  },
  stockChangeForm: {
    commodityId: '',
    scene: 6, // 业务场景
    quantity: '', // 调整数量
    remark: '', // 调整原因
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  supplierList: [],
});
const rules = reactive<FormRules>({
  name: [{required: true, message: '不能为空', trigger: 'blur'}],
  stockNo: [{required: true, message: '不能为空', trigger: 'blur'}],
});
// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  if (type === 'edit') {
    state.dialog.title = '修改商品';
    state.dialog.submitTxt = '修 改';
  }
  nextTick(() => {
    state.ruleForm = Object.assign(state.ruleForm, row);
    state.stockChangeForm.commodityId = state.ruleForm.commodityId;
  });
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = () => {
  stockApi()
      .updateSaleQuantity(state.stockChangeForm)
      .then((res) => {
        if (res && res.code === 200) {
          ElMessage.success(res.msg || '修改成功');
          closeDialog();
          emit('refresh');
        }
      });
};

// 页面加载时
onMounted(() => {
});

// 暴露变量
defineExpose({
  openDialog,
});
</script>

<style>
.stock-change-form-title {
  width: 100%;
  text-align: center;
  font-weight: bold;
  font-size: 1rem;
  margin-bottom: 20px;
}
</style>
